package com.cmpe.group6.storyroad.ListAdapters;

import java.util.HashMap;
import java.util.List;

import com.cmpe.group6.storyroad.R;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

/** Expandable List Adapter for showing linked story fragments on
 * 	read story view.
 */
public class StoryAdapter extends BaseExpandableListAdapter {

	private Context _context;
	private List<String> _listDataHeader; // header titles
	private List<Integer> _listDataId;
	// child data in format of header title, child title
	private HashMap<String, String> _listDataChild;

	/**
	 * @param context Current Context.
	 * @param listDataHeader Titles of the fragments.
	 * @param listChildData Bodies of the fragments.
	 * @param listDataId Ids of the fragments.
	 */
	public StoryAdapter(Context context, List<String> listDataHeader,
			HashMap<String, String> listChildData, List<Integer> listDataId) {
		this._context = context;
		this._listDataHeader = listDataHeader;
		this._listDataChild = listChildData;
		this._listDataId = listDataId;
	}

	@Override
	public Object getChild(int groupPosition, int childPosititon) {
		return this._listDataId.get(groupPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	/* (non-Javadoc)
	 * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getChildView(int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		final String childText = (String) this._listDataChild
				.get(this._listDataHeader.get(groupPosition));
		;

		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) this._context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.story_body_design,
					null);
		}

		TextView txtListChild = (TextView) convertView
				.findViewById(R.id.lblListItem);
		
		int lineCount = childText.length()/50+1;
		lineCount += childText.split("\n").length-1;
		txtListChild.setLines(lineCount);
		
		txtListChild.setText(childText);
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return this._listDataId.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return this._listDataHeader.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	/* (non-Javadoc)
	 * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		String headerTitle = (String) this._listDataHeader.get(groupPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) this._context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.story_title_design,
					null);
		}

		TextView lblListHeader = (TextView) convertView
				.findViewById(R.id.lblListHeader);
		lblListHeader.setTypeface(null, Typeface.BOLD);
		lblListHeader.setText(headerTitle);

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}